﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BookShop.DAL;
using BookShop.DTO;

namespace BookShop.BLL
{
    public class RateCommentService
    {
        public static RateComment Add(RateComment rateComment)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.Add(rateComment);
            }
        }

        public static RateComment Update(RateComment rateComment)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.Update(rateComment);
            }
        }

        public static bool Delete(RateComment rateComment)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.Delete(rateComment);
            }
        }

        public static RateComment GetById(long rateCommentId)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.Find(p => p.Id == rateCommentId);
            }
        }

        public static List<RateComment> GetByBookId(long bookId)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.FindAll(r => r.BookId == bookId).ToList();
            }
        }

        public static List<RateComment> GetByUserId(long userId)
        {
            using (var repository = new RateCommentRepository())
            {
                return repository.FindAll(r => r.CustomerId == userId).ToList();
            }
        }

        public static List<RateComment> Search(long? rateCommentId, long? bookId, long? userId, string content,
            string fromCreateTime, string toCreateTime, string fromRatePoint, string toRatePoint, int? status, int page,
            int pageSize, out int count)
        {
            var repository = new RateCommentRepository();
            var returnList = new List<RateComment>();
            if (rateCommentId.HasValue)
            {
                var rateComment = GetById(rateCommentId.Value);
                if (rateComment != null)
                {
                    returnList.Add(rateComment);
                }
            }
            else
            {
                returnList = repository.GetAll().ToList();
                if (bookId.HasValue)
                {
                    returnList = returnList.FindAll(r => r.BookId == bookId.Value);
                }
                if (userId.HasValue)
                {
                    returnList = returnList.FindAll(r => r.CustomerId == userId.Value);
                }
                if (status.HasValue)
                {
                    returnList = returnList.FindAll(r => r.Status == status.Value);
                }
                if (!string.IsNullOrWhiteSpace(content))
                {
                    returnList = returnList.FindAll(r => r.CommentContent.ToLower().Contains(content.Trim().ToLower()));
                }
                DateTime newFromCreateTime;
                var isValidFromCreateTime = DateTime.TryParse(fromCreateTime, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newFromCreateTime);
                DateTime newToCreateTime;
                var isValidToCreateTime = DateTime.TryParse(toCreateTime, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newToCreateTime);
                if (isValidFromCreateTime && isValidToCreateTime)
                {
                    returnList =
                        returnList.FindAll(a => a.CreateTime >= newFromCreateTime && a.CreateTime <= newToCreateTime);
                }
                if (isValidFromCreateTime && !isValidToCreateTime)
                {
                    returnList =
                        returnList.FindAll(a => a.CreateTime >= newFromCreateTime);
                }
                if (!isValidFromCreateTime && isValidToCreateTime)
                {
                    returnList =
                        returnList.FindAll(a => a.CreateTime <= newToCreateTime);
                }
                double newFromRatePoint;
                var isValidFromRatePoint = double.TryParse(fromRatePoint, NumberStyles.AllowDecimalPoint,
                    CultureInfo.CreateSpecificCulture("en-US"), out newFromRatePoint);
                double newToRatePoint;
                var isValidToRatePoint = double.TryParse(toRatePoint, NumberStyles.AllowDecimalPoint,
                    CultureInfo.CreateSpecificCulture("en-US"), out newToRatePoint);
                if (isValidFromRatePoint && isValidToRatePoint)
                {
                    returnList =
                        returnList.FindAll(o => o.RatePoint >= newFromRatePoint && o.RatePoint <= newToRatePoint);
                }
                if (isValidFromRatePoint && !isValidToRatePoint)
                {
                    returnList =
                        returnList.FindAll(o => o.RatePoint >= newFromRatePoint);
                }
                if (!isValidFromRatePoint && isValidToRatePoint)
                {
                    returnList =
                        returnList.FindAll(o => o.RatePoint <= newToRatePoint);
                }
            }
            count = returnList.Count;
            return returnList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }
    }
}
